#!/usr/bin/perl
################################################################################
#
# Carries out all validation (or all validation which we can handle "simply").
#
#   validate.pl [logFile]
################################################################################

#-------------------------------------------------------------------------------
use strict;
use warnings;
use Cwd;
use Cwd 'abs_path';
use File::Basename;
use File::Copy;
use File::Find;
use File::Spec;



#-------------------------------------------------------------------------------
my $scriptFolder = dirname(File::Spec->rel2abs( __FILE__ ));
require "$scriptFolder/jamieSharedFunctions.pl";



#-------------------------------------------------------------------------------
my $filePattern;
my $logFile = ""; if (scalar @ARGV > 0) { $logFile = File::Spec->catfile($ARGV[0]); }
my $operatingSystem = getOperatingSystem();
my $programFolder;
my $teeScript = "\"$scriptFolder/tee.pl\"";
my $topFolder = cwd;
my @fileList;



#-------------------------------------------------------------------------------
if ("" ne $logFile)
{
  unlink($logFile);
  if ("Windows" eq $operatingSystem) { $logFile = "2>&1 | perl $teeScript \"$logFile\""; }
  else { $logFile = "2>&1 | tee -a \"$logFile\""; }
}



#-------------------------------------------------------------------------------
$filePattern = ".*epub\$";
$programFolder = File::Spec->catfile("$scriptFolder/Validate_ePub_$operatingSystem");
@fileList = ();

find(\&getFiles, $topFolder);
foreach (@fileList)
{
  my $s = File::Spec->catfile($_);
  system("echo Processing $s ... $logFile");
  my $status = system("cd \"$programFolder\" && java -jar epubcheck-1.2.jar \"$s\" $logFile");
}



#-------------------------------------------------------------------------------
$filePattern = ".*xhtml\$";
$programFolder = File::Spec->catfile("$scriptFolder/Validate_Xml_$operatingSystem");
@fileList = ();

find(\&getFiles, $topFolder);
foreach (@fileList)
{
  my $s = File::Spec->catfile($_);
  if (-1 == index($s, "Proofer"))
  {
    system("echo Processing $s ... $logFile");
    my $status = system("cd \"$programFolder\" && ./xmlLint --noout \"$s\" $logFile");
  }
}


#-------------------------------------------------------------------------------
$filePattern = ".*xml\$";
$programFolder = File::Spec->catfile("$scriptFolder/Validate_Xml_$operatingSystem");
@fileList = ();

find(\&getFiles, $topFolder);
foreach (@fileList)
{
  my $s = File::Spec->catfile($_);
  if (-1 != index($s, "OSIS") && -1 == index($s, "Config"))
  {
    system("echo Processing $s ... $logFile");
    my $status = system("cd \"$programFolder\" && ./xmlLint --noout --schema \"http://www.bibletechnologies.net/osisCore.2.1.1 - 06March2006.xsd\" \"$s\" $logFile");
  }
}



#-------------------------------------------------------------------------------
$filePattern = ".*xhtml\$";
$programFolder = File::Spec->catfile("$scriptFolder/Validate_Xml_$operatingSystem");
@fileList = ();

find(\&getFiles, $topFolder);
foreach (@fileList)
{
  my $s = File::Spec->catfile($_);
  if (-1 != index($s, "Proofer"))
  {
    system("echo Processing $s ... $logFile");
    my $status = system("cd \"$programFolder\" && ./xmlLint --noout \"$s\" $logFile");
  }
}



#-------------------------------------------------------------------------------
system("echo Processing completed successfully $logFile");



#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Recursively obtains all files matching filePattern.

sub getFiles
{
  my $file = $File::Find::name;
  if (-f $file && $file =~ /$filePattern/ && -1 == index($file, "/_")) { push(@fileList, $file); }
}    
